<template>
  <div class="service-block">
    <div class="grow-wrapper">
      <div class="content-center">
        <div style="margin: 0 0 10px 0">
          <a-form
            :model="pageInfo"
            name="horizontal_login"
            layout="inline"
            autocomplete="off"
          >
            <a-form-item label="是否通过">
              <JDictSelectTag v-model:value="pageInfo.passed" dictCode="exam_result"
                              placeholder="是否通过" style="width: 200px;" />
            </a-form-item>
            <a-form-item>
              <a-space size="large">
                <a-button type="primary" @click="onSearch" :icon="h(SearchOutlined)">查询</a-button>
              </a-space>
            </a-form-item>
          </a-form>
        </div>
        <a-spin :spinning="loading">
          <template v-if="userExamDetailItems?.length > 0">
            <a-table
              :columns="columns"
              :data-source="userExamDetailItems"
              :loading="loading"
              :pagination="paginationTable"
              @change="handleTableChange"
              bordered
            >
              <template #bodyCell="{ record,column }">
                <template v-if="column.key === 'action'">
                  <a-button type="primary" size="small"
                            @click="router.push(`/exam/userExamResult/examDetail/${record.id}`)">
                    查看详情
                  </a-button>
                </template>
                <template v-else-if="column.key === 'accuracy'">
                  {{ record.accuracy || 0 }}%
                </template>
                <template v-else-if="column.key === 'passed'">
                  <a-tag v-if="record.passed === 1" color="#87d068">通过</a-tag>
                  <a-tag v-else-if="record.passed === 0" color="#f50">未通过</a-tag>
                </template>
              </template>
            </a-table>
          </template>
          <EmptyData v-else />
        </a-spin>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref, h } from "vue";
import { apiUserExamDetailList } from "@/api/exam";
import { useRoute, useRouter } from "vue-router";
import { SearchOutlined, SyncOutlined } from "@ant-design/icons-vue";
import EmptyData from "@/components/public/EmptyData.vue";
import JDictSelectTag from "@/components/public/JDictSelectTag.vue";

const router = useRouter();

const userExamDetailItems = ref<any>([]);
const total = ref<number>(0);
const loading = ref<boolean>(false);
const route = useRoute();
const examId: any = route.params?.id ?? -1;

const paginationTable = reactive({
  total: 0,
  current: 1,
  pageSize: 10,//每页中显示10条数据
  showTotal: total => `共有 ${total} 条数据`  //分页中显示总的数据
});

const columns = [
  {
    title: "考试名称",
    dataIndex: "examTitle",
    width: "20%"
  },
  {
    title: "考试时间",
    dataIndex: "createTime",
    width: "20%"
  },
  {
    title: "交卷时间",
    dataIndex: "commitTime",
    width: "20%"
  },
  {
    title: "考试用时",
    align: "center",
    dataIndex: "userTime",
    width: "10%"
  },
  {
    title: "得分",
    align: "center",
    dataIndex: "userScore",
    width: "10%"
  },
  {
    title: "及格分",
    align: "center",
    dataIndex: "qualifyScore",
    width: "10%"
  },
  {
    title: "阅卷状态",
    align: "center",
    dataIndex: "state",
    width: "10%"
  },
  {
    title: "是否通过",
    align: "center",
    dataIndex: "passed",
    key: "passed",
    width: "10%",
  },
  {
    title: "操作",
    key: "action",
    dataIndex: "action"
  }
];

async function handleTableChange(pagination) {
  paginationTable.current = pagination.current;
  paginationTable.pageSize = pagination.pageSize;
  pageInfo.value.pageNo = pagination.current;
  pageInfo.value.pageSize = pagination.pageSize;
  await getUserExamDetailList();
}

const pageInfo = ref<{
  examId: string;
  pageNo: number;
  pageSize: number;
  passed: string;
}>({
  examId: "",
  // 当前页码
  pageNo: 1,
  // 当前每页显示多少条数据
  pageSize: 10,
  passed: ""
});

async function getUserExamDetailList() {
  loading.value = true;
  userExamDetailItems.value = [];
  pageInfo.value.examId = examId;
  const data: any = await apiUserExamDetailList(pageInfo.value);
  userExamDetailItems.value = data.result?.records;
  total.value = data.result?.total;
  loading.value = false;
}

function onSearch() {
  pageInfo.value.pageNo = 1;
  getUserExamDetailList();
}

onMounted(() => {
  getUserExamDetailList();
});

</script>
<style lang="less" scoped>
.service-block {
  width: 100%;
  height: 87.6vh;
}

.grow-wrapper {
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: space-between;
}

.content-center {
  margin: 0 10px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  width: 100%;
}

:deep(.ant-card-body) {
  padding: 10px;
}

a {
  color: black;
}

a:hover {
  color: #3370ff;
}

</style>
